<template>
  <div class="app-container">
    <el-form size="mini" :inline="true" :model="queryParams" @keyup.enter.native="loadData()">
      <el-form-item label="仓库">
        <el-select v-model="queryParams.wareId" placeholder="请选择仓库" clearable>
          <el-option :label="item.name" :value="item.id" v-for="item in wareList" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="请选择状态">
          <el-option
            v-for="dict in dict.type.purchase_detail_status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="loadData">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="selections.length === 0"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table
      :data="dataList"
      border
      v-loading="loading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="skuName" header-align="center" align="center" label="商品名称">
        <template slot-scope="scope">
          <el-tooltip v-if="scope.row.skuName" class="item" effect="dark" :content="scope.row.skuName" placement="top-start">
            <span> {{ scope.row.skuName.length <= 20 ? scope.row.skuName : (scope.row.skuName.substr(0,20) + '...')}} </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="skuNum" header-align="center" align="center" label="采购数量"></el-table-column>
      <el-table-column prop="wareName" header-align="center" align="center" label="目标仓库"></el-table-column>
      <el-table-column prop="finishTime" header-align="center" align="center" label="完成时间"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.purchase_detail_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="small" v-if="scope.row.status !== 2" @click="handleUpdate(scope.row.id)">修改</el-button>
          <el-button type="text" icon="el-icon-check" v-if="scope.row.status === 1" size="small" @click="handleFinish(scope.row.id)">完成</el-button>
          <el-button type="text" icon="el-icon-delete" v-if="scope.row.status === 0" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
          <span v-if="scope.row.status === 2">-</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="loadData"
    />
    <purchase-detail-form ref="dataForm" @ok="loadData"></purchase-detail-form>
  </div>
</template>

<script>
import purchaseDetailForm from './purchaseDetailForm.vue'
import {deletePurchaseDetail, purchaseDetailList, updatePurchaseDetail} from '@/api/ware/purchaseDetail'
import {wareList} from "@/api/ware/ware";

export default {
  dicts: ['purchase_detail_status'],
  data() {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      total: 0,
      wareList: [],
      dataList: [],
      loading: false,
      selections: [],
      purchaseId: '',
    }
  },
  components: {
    purchaseDetailForm
  },
  created() {
    this.purchaseId = this.$route.query.purchaseId
    this.queryParams.purchaseId = this.$route.query.purchaseId
    this.loadData()
    this.getWares()
  },
  methods: {
    handleFinish(id) {
      let data = {
        id,
        status: 2
      }
      updatePurchaseDetail(data).then(res => {
        if (res.success) {
          this.loadData()
          this.$message.success('采购清单已完成')
        }
      })
    },

    handleAdd() {
      this.$refs.dataForm.add(this.purchaseId)
    },

    handleUpdate(id) {
      this.$refs.dataForm.edit(id)
    },

    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
      }
      this.loadData()
    },

    getWares() {
      let params = {
        pageNo: 1,
        pageSize: 500
      }
      wareList(params).then(res => {
         if (res.success) {
           this.wareList = res.data.reocrds
         }
      })
    },
    // 获取数据列表
    loadData() {
      this.loading = true
      purchaseDetailList(this.queryParams).then(res => {
        if (res.success) {
          this.dataList = res.data.records
          this.totalPage = res.data.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.loading = false
      })
    },

    selectionChangeHandle(val) {
      this.selections = val
    },

    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },

    handleDelete(id) {
      var ids = id
        ? [id]
        : this.selections.map(item => {
          return item.id
        })
      this.$confirm(
        `确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        deletePurchaseDetail(ids).then(res => {
          if (res.success) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.loadData()
              }
            })
          }
        })
      })
    }
  }
}
</script>
